// pages/shop/shop.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //城市
    bdcity: '',

    //轮播图
    imgUrls: [
      '/images/spbbb.jpg',
      '/images/spbbb.jpg',
      '/images/spbbb.jpg'
    ],
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,  //是否自动切换
    interval: 4000,  //自动切换时间间隔
    circular: true,  //是否采用衔接滑动
    //end

    //倒计时
    endTime: '2020-11-22 10:40:30',//2020/11/22 10:40:30这种格式也行
    //商城分类
    shangClassif:[],
    //秒杀推荐
    seckill:[],
    //热销
    hot:[],
    //好店
    shop:[],
    //便宜
    cheap:[],
    //推荐
    recommend:[],
    //导航栏
    nav:[],
    page:"1",
    limit:"6"

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var page = that.data.page;
    var limit = that.data.limit;
    that.countDown()
    //秒杀列表 没用
    app.wxRequest('get','/Samecity/class/classification',{},function(res){
      console.log(res)
      if(res.data.code == 1){
        that.setData({
          shangClassif:res.data.data
        })
      }
    },function(err){

    }),
    //秒杀推荐
    app.wxRequest('get','/home/goods/seckill_recommend',{'class_id':'1'},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          seckill:res.data.data
        })
      }
    },function(err){
    }),
    //好店
    app.wxRequest('get','/home/goods/cakes?class_id=5',{},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          shop:res.data.data
        })
      }
    }),
    //热销推荐
    app.wxRequest('get','/home/goods/cakes',{},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          hot:res.data.data
        })
      }
    }),
    //便宜好货
    app.wxRequest('get','/home/goods/seckill_recommend?class_id=4',{},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          cheap:res.data.data
        })
      }
    }),
    //为你推荐
    app.wxRequest('get','/home/goods/recommend',{'page':page,'limit':limit},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          recommend:res.data.data
        })
      }
    })
    //导航栏
    app.wxRequest('get','/home/goods/nav',{},function(res){
      console.log(res)
      if(res.data.code==1){
        that.setData({
          nav:res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var city = wx.getStorageSync('city')
    if (!city) {
      app.setCity(function (res) {
        console.log(res);
        if (res == 'ok') {
          city = wx.getStorageSync('city')
          that.setData({
            bdcity: city
          })
        }
      })
    } else {
      that.setData({
        bdcity: city
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(res);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 倒计时
  countDown: function () {
    var that = this;
    var nowTime = new Date().getTime();//现在时间（时间戳）
    var endTime = new Date(that.data.endTime).getTime();//结束时间（时间戳）
    var time = (endTime - nowTime) / 1000;//距离结束的毫秒数
    // 获取天、时、分、秒
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    // console.log(day + "," + hou + "," + min + "," + sec)
    day = that.timeFormin(day),
      hou = that.timeFormin(hou),
      min = that.timeFormin(min),
      sec = that.timeFormin(sec)
    that.setData({
      day: that.timeFormat(day),
      hou: that.timeFormat(hou),
      min: that.timeFormat(min),
      sec: that.timeFormat(sec)
    })
    // 每1000ms刷新一次
    if (time > 0) {
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    } else {
      that.setData({
        countDown: false
      })
    }
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0 : param;
  },
  


})